﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpNewFeaturesUse
{
    public class ReadOnly
    {
        private bool _ro_const = false;
        const bool ro_readonly = true;
        private int _ro = 1;
        public int ro
        {
            get => _ro;
            set
            {
                if (_ro_const) throw new InvalidOperationException();
                else _ro = value;
            }
        }
        public ReadOnly()
        {
            ro = 0;

            if (ro_readonly) _ro_const = true; 
        }
        public void ShowUse()
        {
            try
            {
                ro = 2;
            }
            catch (InvalidOperationException ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.WriteLine("Read Only属性只能在声明时和初始化时赋值");
        }
    }
}